//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Canada.h"
using namespace Cephei::QL::Times::Calendars;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Calendars::CCanada::CCanada (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::Canada::MarketEnum>^ market) : CCalendar(CCanada::typeid)
{
    try
    {
#ifdef HANDLE
        _phCanada = NULL;
#endif
        QuantLib::Canada::Market _market = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::Canada::MarketEnum>::IsSome::get (market) ? (QuantLib::Canada::Market)market->Value : QuantLib::Canada::Market::Settlement); //10
        _ppCanada = new boost::shared_ptr<QuantLib::Canada> (new QuantLib::Canada ( _market ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::Calendars::CCanada::CCanada (boost::shared_ptr<QuantLib::Canada>& childNative, Object^ owner) : CCalendar(CCanada::typeid)
{
#ifdef HANDLE
	_phCanada = NULL;
#endif
	_ppCanada = &childNative;
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
}
Cephei::QL::Times::Calendars::CCanada::CCanada (QuantLib::Canada& childNative, Object^ owner) : CCalendar(CCanada::typeid)
{
#ifdef HANDLE
	_phCanada = NULL;
#endif
	_ppCanada = new boost::shared_ptr<QuantLib::Canada> (&childNative);
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
    _CanadaOwner = owner;
    _CalendarOwner = owner;
}

Cephei::QL::Times::Calendars::CCanada::CCanada (CCanada^ copy) : CCalendar(CCanada::typeid)
{
#ifdef HANDLE
	_phCanada = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCanada = new boost::shared_ptr<QuantLib::Canada> (copy->GetShared());
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
    }
}
Cephei::QL::Times::Calendars::CCanada::CCanada (PLATFORM::Type^ t) : CCalendar(CCanada::typeid)
{
#ifdef HANDLE
	_phCanada = NULL;
#endif
	if (!t->IsSubclassOf(CCanada::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Calendars::CCanada::CCanada (QuantLib::Handle<QuantLib::Canada>& childNative, Object^ owner)  : CCalendar(CCanada::typeid)
{
	_phCanada = &childNative;
	_ppCanada = &static_cast<boost::shared_ptr<QuantLib::Canada>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
    _CanadaOwner = owner;
}
Cephei::QL::Times::Calendars::CCanada::CCanada (QuantLib::Handle<QuantLib::Canada> childNative)  : CCalendar(CCanada::typeid)
{
	_phCanada = &childNative;
	_ppCanada = &static_cast<boost::shared_ptr<QuantLib::Canada>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Calendars::CCanada::CCanada (QuantLib::Canada childNative)  : CCalendar(CCanada::typeid)
{
#ifdef HANDLE
	_phCanada = NULL;
#endif
	_ppCanada = new boost::shared_ptr<QuantLib::Canada> (new QuantLib::Canada (childNative));
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppCanada));
}
#endif

Cephei::QL::Times::Calendars::CCanada::~CCanada ()
{
    if (_ppCanada != NULL)
    {
	    delete _ppCanada;
        _ppCanada = NULL;
    }
}
Cephei::QL::Times::Calendars::CCanada::!CCanada ()
{
    if (_ppCanada != NULL)
    {
	    delete _ppCanada;
    }
}
QuantLib::Canada& Cephei::QL::Times::Calendars::CCanada::GetReference ()
{
    if (_ppCanada == NULL) throw REFNEW NativeNullException ();
	return **_ppCanada;
}
boost::shared_ptr<QuantLib::Canada>& Cephei::QL::Times::Calendars::CCanada::GetShared ()
{
    if (_ppCanada == NULL) throw REFNEW NativeNullException ();
	return *_ppCanada;
}
QuantLib::Canada* Cephei::QL::Times::Calendars::CCanada::GetPointer ()
{
    if (_ppCanada == NULL) throw REFNEW NativeNullException ();
	return &**_ppCanada;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Canada>& Cephei::QL::Times::Calendars::CCanada::GetHandle ()
{
	if (_phCanada == NULL)
	{
		_phCanada = new Handle<QuantLib::Canada> (*_ppCanada);
	}
	return *_phCanada;
}
#endif
bool Cephei::QL::Times::Calendars::CCanada::HasNative () 
{
	return (_ppCanada != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Calendars::ICanada^ Cephei::QL::Times::Calendars::CCanada_Factory::Create (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::Canada::MarketEnum>^ market)
{
    return REFNEW CCanada ( market);
}
